<?php

include("../utility/connection.php");
include("../model/mGammu.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
    $NoHp = trim(mysql_real_escape_string($_POST["txtNoHp"]));
    $Msg = trim(mysql_real_escape_string($_POST["txtMessage"]));
    $Sender = trim(mysql_real_escape_string($_POST["hidSender"]));
    
    $KodeSms = trim(mysql_real_escape_string($_POST["hidKodeSms"]));
    $AlasanSms = trim(mysql_real_escape_string($_POST["hidAlasanSms"]));
    
    $pageCode = $_GET["pageCode"];
    
    //exec('C:\Program Files\Gammu 1.32.0\bin\gammu-smsd-inject.exe -c C:\Program Files\Gammu 1.32.0\bin\smsdrc EMS '.$NoHp.' -text "'.$Msg.'"');
    //$result = fnInsertMsg($NoHp, $Msg, $Sender);
    $result = fnMyOutbox($NoHp, $Msg, $Sender, $KodeSms, $AlasanSms, $pageCode);
    if ($result) {
        $command = '"C:\Program Files\Gammu 1.32.0\bin\gammu.exe" -c "C:\Program Files\Gammu 1.32.0\bin\gammurc" sendsms text '.$NoHp.' -text "'.$Msg.'"';
        $retMessage = exec($command);
        $_SESSION['MsgSent'] = 'Command: '.$command.', Result: '.$retMessage;
        if ($pageCode == '2') {
            header("location: ../ui/admin/smsForm.php?init=0");
        } else if ($pageCode == '1') {
            header("location: ../ui/reviewer/smsFormReviewer.php?init=0");
        }
        //echo 'Command: '.$command.', Result: '.$retMessage;
    } else {
        $retMessage = 'Gagal Insert ke Table mst_sms_outbox!';
        $_SESSION['MsgSent'] = $retMessage;
        if ($pageCode == '2') {
            header("location: ../ui/admin/smsForm.php?init=0");
        } else if ($pageCode == '1') {
            header("location: ../ui/reviewer/smsFormReviewer.php?init=0");
        }
        //echo $retMessage;
    }
    
}

?>
